using Sirenix.OdinInspector;
using Unity.VisualScripting;
using UnityEngine;

public class CharactorManager : MonoBehaviour
{
    [Required("需要操作目标对象")]
    [InfoBox("操作目标对象上需要挂载 CharacterController 组件")]
    [LabelText("操作目标对象")]
    public Transform charactor;
    [LabelText("水平垂直移动速度")]
    public float speed = 5f;
    [LabelText("水平垂直移动冲刺速度")]
    public float sprintSpeed = 10;
    [LabelText("转向灵敏度")]
    public float mouseSensitivity = 3f;
    [LabelText("控制模式")]
    public ControlType controlType;
    [LabelText("重力加速度"), ShowIf("controlType", ControlType.Floor)]
    public float gravity = -9.81f;
    [LabelText("跳跃高度"), ShowIf("controlType", ControlType.Floor)]
    public float jumpHeight = 1f;

    private CharacterController characterController;
    private float rotationX;
    private float rotationY;
    private float velocity; // 存储垂直速度
    
    private void Start()
    {
        if(charactor == null)
        {
            charactor = transform;
        }
        characterController = charactor.GetComponent<CharacterController>() == null ? charactor.AddComponent<CharacterController>() : charactor.GetComponent<CharacterController>();
        rotationX = charactor.localEulerAngles.x;
        rotationY = charactor.localEulerAngles.y;
    }

    private void Update()
    {
        // 处理水平移动
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");

        // 应用移动速度（按住左 Shift 时加速）
        float currentSpeed = Input.GetKey(KeyCode.LeftShift) ? sprintSpeed : speed;
        Vector3 move = charactor.TransformDirection(new(horizontal, 0, vertical)) * currentSpeed * Time.deltaTime;
        if(controlType == ControlType.Fly)
        {
            if (Input.GetKey(KeyCode.Q))
                move.y -= currentSpeed * Time.deltaTime;
            if (Input.GetKey(KeyCode.E))
                move.y += currentSpeed * Time.deltaTime;

            velocity = 0; // 飞行时禁用重力
        }
        else
        {
            // 应用重力
            if (characterController.isGrounded && velocity < 0)
            {
                velocity= -2f; // 小值确保角色稳定在地面
            }
            velocity += gravity * Time.deltaTime;
            // 处理跳跃
            if (characterController.isGrounded && Input.GetButtonDown("Jump"))
            {
                velocity = Mathf.Sqrt(jumpHeight * -2f * gravity);
            }
            move.y = velocity * Time.deltaTime;
        }
        characterController.Move(move);

        if (Input.GetMouseButtonDown(1)) 
        {
            rotationX = charactor.localEulerAngles.x;
            rotationY = charactor.localEulerAngles.y;
            if (rotationX > 180) rotationX -= 360;
        }

        if (Input.GetMouseButton(1)) 
        {
            float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity;
            float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity;

            // 绕Y轴旋转（左右拖拽）
            rotationY += mouseX;
            // 绕X轴旋转（上下拖拽）
            rotationX -= mouseY;
            if (rotationX > 180) rotationX -= 360;
            rotationX = Mathf.Clamp(rotationX, -90f, 90f);
            charactor.localRotation = Quaternion.Euler(rotationX, rotationY, 0f);
        }
    }

    public enum ControlType
    {
        [LabelText("飞行模式")]
        Fly,
        [LabelText("重力模式")]
        Floor
    }
}
